/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.googlecode.jsimplemarket.wicket.pages;

//import com.googlecode.jmicroerp.model.Recipient;
//import com.googlecode.jmicroerp.wicket.magazin.WizardPage;
import com.googlecode.jsimplemarket.model.Creanta;
import com.googlecode.jsimplemarket.model.Firma;
import com.googlecode.jsimplemarket.model.Inchiriere;
import com.googlecode.jsimplemarket.model.Plata;
import com.googlecode.jsimplemarket.model.SpatiuComercial;
import com.googlecode.jsimplemarket.model.NStareSpatiuComercial;
import com.googlecode.jsimplemarket.model.Taxa;
import com.googlecode.jsimplemarket.service.AbsentaService;
import com.googlecode.jsimplemarket.service.GenericService;
import com.googlecode.jsimplemarket.service.InchiriereService;
import com.googlecode.jsimplemarket.service.TaxaService;

import java.math.BigDecimal;
//import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;



//import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.security.core.GrantedAuthority;

/**
 *
 * @author Alexandru.Codescu
 */
public class TarabaViewPage extends DefaultViewPage {

    @SpringBean(name = "genericService")
    private GenericService<SpatiuComercial> spatiuComercialService;
    
    @SpringBean(name = "genericService")
    private GenericService<NStareSpatiuComercial> stareSpatiuComercial;
    
    
    @SpringBean(name = "inchiriereService")
    private InchiriereService inchiriereService;
    
    @SpringBean(name = "genericService")
    private GenericService<Plata> plataService;
    @SpringBean(name = "genericService")
    private GenericService<Creanta> creantaService;
    @SpringBean(name = "genericService")
    private GenericService<Inchiriere> serviceInchiriere;
    @SpringBean(name = "genericService")
    private GenericService<Firma> firmaService;
    @SpringBean(name = "taxaService")
    private TaxaService taxaService;
    @SpringBean(name = "absentaService")
    private AbsentaService absentaService;
    
    private Date dataPlata = new Date();
    private Date dataStart = getFirstDayOfMonth();
    private Date dataStop = getLastDayOfMonth();
    /*private String getOcupant(SpatiuComercial spatiuComercial) {
        if (spatiuComercial.getInchiriere() != null) {
        	return "";
            //return spatiuComercial.getInchiriere().getFirma().getDenumire();
        }
        return "";
    }

    private String getContact(SpatiuComercial spatiuComercial) {
        if (spatiuComercial.getInchiriere() != null) {
            return ""; 
            //return spatiuComercial.getInchiriere().getFirma().getDateContact();
        }
        return "";
    }*/
    
    /*private Link updateLink(String id, final Long idStare, final Long idSpatiu) {
        Link link = new Link(id) {
            @Override
            public void onClick() {
            	
            	SpatiuComercial sc = spatiuComercialService.getById(SpatiuComercial.class, idSpatiu);
            	sc.setStareSpatiuComercial(stareSpatiuComercial.getById(NStareSpatiuComercial.class, idStare));
            	spatiuComercialService.update(sc);         	
            	
                setResponsePage(new TarabaViewPage(idSpatiu));
            }
        };

        return link;
    }*/

    private Date getDataSfarsit(Date dataInceput, int interval) {
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(dataInceput);
        gregorianCalendar.add(GregorianCalendar.DATE, interval);
        
        return gregorianCalendar.getTime();
    }
    
    private Date getDataInceputZi(Date data) {
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(data);
        gregorianCalendar.set(GregorianCalendar.HOUR, 0);
        gregorianCalendar.set(GregorianCalendar.MINUTE, 0);
        gregorianCalendar.set(GregorianCalendar.SECOND, 0);
        gregorianCalendar.set(GregorianCalendar.AM_PM, GregorianCalendar.AM);
        return gregorianCalendar.getTime();
    }
    
    private Date getDataSfarsitZi(Date data) {
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(data);
        gregorianCalendar.set(GregorianCalendar.AM_PM, GregorianCalendar.PM);
        gregorianCalendar.set(GregorianCalendar.HOUR, 11);
        gregorianCalendar.set(GregorianCalendar.MINUTE, 59);
        gregorianCalendar.set(GregorianCalendar.SECOND, 59);
        return gregorianCalendar.getTime();
    }
    
    private GregorianCalendar getTodayCalendar() {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
        calendar.set(GregorianCalendar.MINUTE, 0);
        calendar.set(GregorianCalendar.SECOND, 0);
        //calendar.set(GregorianCalendar.AM_PM, GregorianCalendar.AM);
        calendar.set(GregorianCalendar.MILLISECOND, 0);
        return calendar;
    }
    
    private Date getTodayDate() {
        return getTodayCalendar().getTime();
    }
    
    private Date addDays(Date date, int days) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
                 
        return cal.getTime();
    }
    
    private Date get7DayDate() {
        GregorianCalendar calendar = getTodayCalendar();
        calendar.add(GregorianCalendar.DATE, -7);
        
        return calendar.getTime();
    }
    
    private Date getFirstDayOfMonth() {
        GregorianCalendar calendar = getTodayCalendar();
        //comment next line for current month
        calendar.add(GregorianCalendar.MONTH, 1);
        calendar.set(GregorianCalendar.DATE, 1); //first day of the next month
        
        return calendar.getTime();
    }
    
    private Date getLastDayOfMonth() {
        GregorianCalendar calendar = getTodayCalendar();
      //comment next line for current month
        calendar.add(GregorianCalendar.MONTH, 1);
        calendar.set(GregorianCalendar.DATE, 
        		calendar.getActualMaximum(GregorianCalendar.DAY_OF_MONTH)); //last day of the next month
        
        return calendar.getTime();
    }
    public TarabaViewPage(final Long idSpatiuComercial) {
        SpatiuComercial spatiuComercial = spatiuComercialService.getById(SpatiuComercial.class, idSpatiuComercial);
        final Label lblTaraba = new Label("lblTaraba", spatiuComercial.getCod());
        //final Label lblStare = new Label("lblStare", spatiuComercial.getStareSpatiuComercial().getDenumire());
        
        Link lnkPrev = new Link("lnkPrev") {
            @Override
            public void onClick() {
            	if (idSpatiuComercial != 1) {
            		setResponsePage(new TarabaViewPage(idSpatiuComercial-1));
            	}                
            }
        };
    	
        Link lnkNext = new Link("lnkNext") {
            @Override
            public void onClick() {
            	if (idSpatiuComercial != 140) {
            		setResponsePage(new TarabaViewPage(idSpatiuComercial+1));
            	}
            }
        };
        add(lnkPrev, lnkNext);
        /*Link statusLink1 = updateLink("statusLink1", 1L, idSpatiuComercial);
        Link statusLink2 = updateLink("statusLink2", 2L, idSpatiuComercial);
        Link statusLink3 = updateLink("statusLink3", 3L, idSpatiuComercial);
        Link statusLink4 = updateLink("statusLink4", 4L, idSpatiuComercial);
        Link statusLink5 = updateLink("statusLink5", 5L, idSpatiuComercial);
        Link statusLink6 = updateLink("statusLink6", 6L, idSpatiuComercial);
        add(statusLink1,statusLink2,statusLink3,statusLink4,statusLink5,statusLink6);  */      
        
        Object[] sc = inchiriereService.queryFirmaByTaraba(idSpatiuComercial,new Date());
        final Label lblOcupant = new Label("lblOcupant", sc == null ? "---" : sc[0].toString());
        final Label lblContact = new Label("lblContact", sc == null ? "---" : sc[1].toString());
        final Label lblStare = new Label("lblStare", sc == null ? "Disponibila" : sc[4].toString());
        final Label lblCount = new Label("lblCount", sc == null ? "0" : sc[5].toString());
        final Label lblValoare = new Label("lblValoare", sc == null ? "0,00" : sc[6].toString());
        final Label lblDataPlata = new Label("lblDataPlata", sc == null ? "---" : sc[7].toString());
        Object[] absente = null;
       
        if (sc!=null) {
        	absente = absentaService.queryBySpatiuLastMonth(idSpatiuComercial, (Long) sc[3]);
        }
        
        
        final Label lblAbsenta = new Label("lblAbsenta", absente == null ? "---" : absente[0].toString());
        
        //final Label lblAbsenta = new Label("lblAbsenta", absente);

        List<Taxa> taxe = taxaService.queryTaxePlataRapida();
        PlataRapidaListView plataRapidaView = new PlataRapidaListView("plataRapida", taxe, idSpatiuComercial);

        Link lkPlataAnticipata = new Link("lkPlataAnticipata") {

            @Override
            public void onClick() {
                setResponsePage(new PlataAnticipataPage(idSpatiuComercial));
            }
        };

        /*Link lkPlataRezervare = new Link("lkPlataRezervare") {

            @Override
            public void onClick() {
                setResponsePage(new PlataRezervarePage(idSpatiuComercial));
            }
        };*/
        
        Link lkIstoricInchirieri = new Link("lkIstoricInchirieri") {
            @Override
            public void onClick() {
                setResponsePage(new IstoricPlatiPage(idSpatiuComercial));
            }
        };
        
        Link lkIstoricAbsente = new Link("lkIstoricAbsente") {
            @Override
            public void onClick() {
                setResponsePage(new IstoricAbsentePage(idSpatiuComercial));
            }
        };
        
        add(lblTaraba, lblStare, lblOcupant, lblContact, lblAbsenta, lblCount, lblValoare, lblDataPlata,
        		plataRapidaView, lkPlataAnticipata, lkIstoricInchirieri, lkIstoricAbsente);
    }

    public class PlataRapidaListView extends ListView<Taxa> {
        private Long idSpatiu;
        
        public PlataRapidaListView(String id, List<Taxa> list, Long idSpatiu) {
            super(id, list);
            this.idSpatiu = idSpatiu;
        }
        
        private Date getDataSfarsit(Date dataInceput, int interval) {
            GregorianCalendar gregorianCalendar = new GregorianCalendar();
            gregorianCalendar.setTime(dataInceput);
            gregorianCalendar.add(GregorianCalendar.DATE, interval);
        
            return gregorianCalendar.getTime();
        }
        

        @Override
        protected void populateItem(ListItem<Taxa> li) {
            final Taxa taxa = li.getModelObject();
            Link lkPlataRapida = new Link("lkPlataRapida") {

                @Override
                public void onClick() {
                    SpatiuComercial spatiuComercial = spatiuComercialService.getById(SpatiuComercial.class, idSpatiu);
                    Inchiriere inchiriere = new Inchiriere();
                    inchiriere.setDataInceput(new Date());
                    inchiriere.setDataSfarsit(new Date());
                    inchiriere.setSpatiuComercial(spatiuComercial);
                    inchiriere.setTaxe(new ArrayList<Taxa>());
                    inchiriere.getTaxe().add(taxa);
                    serviceInchiriere.insert(inchiriere);
                    
                    Creanta creanta = new Creanta();
                    creanta.setDataInceput(new Date());
                    creanta.setDataSfarsit(getDataSfarsit(creanta.getDataInceput(), 1));
                    creanta.setTaxa(taxa);
                    creanta.setValoare(taxa.getValoare());
                    creanta.setValoareReziduala(BigDecimal.ZERO);
                    creanta.setPlati(new ArrayList<Plata>());
                    creanta.setInchiriere(inchiriere);
                    creantaService.insert(creanta);

                    Plata plata = new Plata();
                    plata.setDataPlata(new Date());
                    plata.setValoare(taxa.getValoare());
                    plata.setCreanta(creanta);
                    plataService.insert(plata);

                    setResponsePage(PiataViewPage.class);
                }
            };
            lkPlataRapida.add(new Label("lblPlataRapida", taxa.getDenumire()));
            li.add(lkPlataRapida);
        }

    }
}
